1 module project.folder_selector; 2 import std.file; 3 4 enum SaveNotSupported = "SaveNotSupported"; 5 6 private bool hasSpace(string name) 7 { 8 foreach(ch; name) 9 if(ch == ' ') 10 return true; 11 return false; 12 } 13 14 private bool isFolderEmpty(string folderPath) 15 { 16 foreach (DirEntry e; dirEntries(folderPath, SpanMode.shallow)) 17 return false; 18 return true; 19 } 20 21 import commons; 22 bool selectFolderForProject(ref Terminal t, out string projectPath, out string error) 23 { 24 projectPath = showSaveFileDialog(t, "Name of your project(Should not contain spaces)", ["HipremeProject"]); 25 if(projectPath.length == 0) 26 { 27 error = "Execution cancelled. No project will be created."; 28 return false; 29 } 30 else if(projectPath.hasSpace) 31 { 32 showErrorMessage(t, "Save Project Error", "Your project name '"~projectPath~"' should not contain spaces"); 33 return selectFolderForProject(t, projectPath, error); 34 } 35 else if(projectPath.exists && isDir(projectPath) && !isFolderEmpty(projectPath)) 36 { 37 error = projectPath~" not empty. Please select an empty folder."; 38 return false; 39 } 40 else if(projectPath[$-1] == '\0') 41 projectPath = projectPath[0..$-1]; 42 return true; 43 } 44 45 version(Windows) 46 { 47 import arsd.minigui; 48 void showErrorMessage(ref Terminal t, string title, string message) 49 { 50 messageBox(title, message, MessageBoxStyle.OK, MessageBoxIcon.Error); 51 } 52 53 string showSaveFileDialog(ref Terminal t, string initialName, string[] filters) 54 { 55 string fName; 56 getSaveFileName((string folderName){fName = folderName;}, initialName, filters); 57 return fName; 58 } 59 } 60 else 61 { 62 version(linux) 63 { 64 import std.process; 65 import std.stdio; 66 private string getCommandToShowError(string title, string message) 67 { 68 if(executeShell("which zenity").output) 69 return "zenity --error --title=\""~title~"\" --text=\""~message~"\""; 70 return ""; 71 } 72 private string getCommandToSaveFileDialog(string title, string filter) 73 { 74 if(executeShell("which zenity").output) 75 return "zenity --file-selection --confirm-overwrite --directory --save --title=\""~ 76 title~"\" --file-filter=\""~filter~" | *\""; 77 return ""; 78 } 79 80 void showErrorMessage(ref Terminal t, string title, string message) 81 { 82 string cmd = getCommandToShowError( title, message); 83 if(cmd.length) 84 executeShell(cmd); 85 else 86 defaultShowErrorMessage(t, title, message); 87 } 88 89 90 string showSaveFileDialog(ref Terminal t, string initialName, string[] filters) 91 { 92 string cmd = getCommandToSaveFileDialog(initialName, filters[0]); 93 if(cmd.length) 94 { 95 string ret = executeShell(cmd).output; 96 import std.string:splitLines; 97 import std.array:join; 98 string[] lines = splitLines(ret); 99 if(lines.length > 1) 100 { 101 executeShell("zenity --warning --title=\"'"~cmd~"' warnings\" --text=\""~join(lines[0..$-1])~"\""); 102 ret = lines[$-1]; 103 } 104 if(ret.length != 0 && ret[$-1] == '\n') ret = ret[0..$-1]; 105 return ret; 106 } 107 return defaultShowSaveFileDialog(t, initialName, filters); 108 } 109 } 110 else 111 { 112 void showErrorMessage(ref Terminal t, string title, string message){defaultShowErrorMessage(t, title,message);} 113 string showSaveFileDialog(ref Terminal t, string initialName, string[] filters){return defaultShowSaveFileDialog(t, initialName, filters);} 114 115 } 116 } 117 118 private void defaultShowErrorMessage(ref Terminal t, string title, string message) 119 { 120 t.writelnError("Error:", title, "\n\t", message); 121 } 122 private string defaultShowSaveFileDialog(ref Terminal t, string initialName, string[] filters) 123 { 124 import std.string : chomp; 125 t.writeln("Write a folder name to create your HipProject: "~ initialName); 126 t.flush; 127 return t.getline("").chomp(); 128 }